package br.com.localizagora.archive;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.com.localizagora.banco.Conexao;

public class StatusDAO {
	
	private static PreparedStatement ps;
	private static Connection con;

	public static void insereStatus(Status status){
		String sql = "INSERT INTO `localizagora`.`status` ( `idDevice`, " +
														  " `combustEletric`," +
														  " `tracking`," +
														  " `alarm`," +
														  " `charge`," +
														  " `acc`," +
														  " `voltage`," +
														  " `signal`," +
														  " `dateTime`," +
														  " `realDateTime`)" +
														  " VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? );";
		conectar(sql);

		try {

			ps.setInt(1, status.getIdDevice());
			ps.setBoolean(2, status.isOilEletricity());
			ps.setBoolean(3, status.isGpsTracking());
			ps.setInt(4, status.getAlarm());
			ps.setBoolean(5, status.isCharge());
			ps.setBoolean(6, status.isAcc());
			ps.setInt(7, status.getVoltageLevel());
			ps.setInt(8, status.getSignalLevel());
			ps.setTimestamp(9, status.getDateTime());	
			ps.setTimestamp(10, status.getRealDateTime());
			ps.execute();
			
			System.out.println("Status ok");

		} catch (SQLException e) {
			// TODO Tratar
			e.printStackTrace();
		}
	}

	private static void conectar(String sql) {
		try {
			con = Conexao.getConnection();
			ps = con.prepareStatement(sql);
		} catch (SQLException ex) {
			Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	private void fechar() {
		try {
			ps.close();
			con.close();
		} catch (SQLException ex) {
			Logger.getLogger(StatusDAO.class.getName()).log(Level.SEVERE, null, ex);
		}
	}    
}
